package com.tenglong.teacher.ui.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.tenglong.teacher.R;
import com.tenglong.teacher.data.resultInfo.BaseHomeWorkInfo;
import com.tenglong.teacher.data.resultInfo.CurrentHomeWorkOutsideInfo;
import com.tenglong.teacher.data.resultInfo.HistoryHomeWorkOutsideInfo;
import com.tenglong.teacher.ui.adapter.viewholder.CurrentHomeworkOutsideViewHolder;
import com.tenglong.teacher.ui.adapter.viewholder.HistoryHomeworkClassHourViewHolder;
import com.tenglong.teacher.ui.adapter.viewholder.HistoryHomeworkOutsideViewHolder;
import com.tenglong.teacher.ui.adapter.viewholder.base.BaseViewHolder;

import java.util.List;

/**
 * @author hdx on 2019/3/1.
 */
public class HomeWorkAdapter extends RecyclerView.Adapter<BaseViewHolder<BaseHomeWorkInfo>> {

    private List<BaseHomeWorkInfo> infos;
    private HomeworkListener listener;

    public HomeWorkAdapter(List<BaseHomeWorkInfo> infos) {
        this.infos = infos;
    }
    private boolean isOld=true;
    @NonNull
    @Override
    public BaseViewHolder<BaseHomeWorkInfo> onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        switch (viewType) {
            case 0:
                return new CurrentHomeworkOutsideViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_current_homework_outside, parent, false),listener);
            case 1:
                return new HistoryHomeworkOutsideViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_history_homework_classes_item, parent, false),listener);
            default:
                return new HistoryHomeworkClassHourViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_history_homework_class_hour_item, parent, false),listener);
        }
    }

    @Override
    public int getItemViewType(int position) {
        BaseHomeWorkInfo info = infos.get(position);
        if (info instanceof CurrentHomeWorkOutsideInfo){
            return 0;
        } else if (info instanceof HistoryHomeWorkOutsideInfo){
            return 1;
        } else {
            return 2;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder<BaseHomeWorkInfo> holder, int position) {
        if(holder instanceof HistoryHomeworkOutsideViewHolder){
            ((HistoryHomeworkOutsideViewHolder) holder).setOld(isOld());
        }
        holder.setLayoutInfo(infos.get(position));
    }

    @Override
    public int getItemCount() {
        return infos.size();
    }

    public void setHomeworkListener(HomeworkListener listener){
        this.listener = listener;
    }

    public void setOld(boolean old) {
        this.isOld = old;
    }

    public boolean isOld() {
        return isOld;
    }

    public interface HomeworkListener{
        void onClickCurrentItemListener(int position);
        void onClickHistoryItemListener(int position);
        void onClickHistoryClassHourItemListener(int position);
    }
}
